package com.sskj.jdcx.service.impl;

import java.io.IOException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.sskj.core.annotation.ControllerLogExeTime;
import com.sskj.jdcx.domain.vo.Param;
import com.sskj.jdcx.domain.vo.ShenHeZhuangTai;

@RestController
@RequestMapping("/wechat/jdcx")
public class JinDuChaXunServiceImpl {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Value("${sskj.conf.jdcxUrl}")
	private String jdcxUrl;
	
	@Value("${sskj.conf.sgxkjd}")
	private String sgxkjd;
	
	@Value("${sskj.conf.sghtbajd}")
	private String sghtbajd;
	
	@Value("${sskj.conf.xmbrybgjd}")
	private String xmbrybgjd;
	
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "**/querystatusDescription", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "根据编号查询施工许可进度")
	public List<ShenHeZhuangTai> querystatusDescription() {
		ShenHeZhuangTai shzt=new ShenHeZhuangTai();
		return shzt.returnList();
	}
	
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "**/querysgxkjd", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "根据编号查询施工许可进度")
	public String querysgxkjd(Param param) {
		String path = sgxkjd;
		return this.returnResult(path,param);
	}
	
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "**/querysghtbajd", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "根据编号查询施工合同备案进度")
	public String querysghtbajd(Param param) {
		String path = sghtbajd;
		return this.returnResult(path,param);
	}
	
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "**/queryxmbrybgjd", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "根据编号查询项目部人员变更")
	public String queryxmbrybgjd(Param param) {
		String path = xmbrybgjd;
		return this.returnResult(path,param);
	}

	private String returnResult(String path,Param bean){
		String lines ="";
		String url=jdcxUrl+path;
		HttpPost httpPost = new HttpPost(url);
		CloseableHttpResponse response = null;
		CloseableHttpClient httpClient = HttpClients.createDefault();
		RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();
		httpPost.setConfig(requestConfig);
		httpPost.addHeader("Content-Type", "application/json");
		try {
		    StringEntity requestEntity = new StringEntity(JSON.toJSONString(bean), "utf-8");
		    httpPost.setEntity(requestEntity);
		    response = httpClient.execute(httpPost, new BasicHttpContext());
		    if (response.getStatusLine().getStatusCode() != 200) {
		    	logger.error("request url failed, http code=" + response.getStatusLine().getStatusCode()
			    		+ ", url=" + url);
		    	lines="{\"code\":\""+response.getStatusLine().getStatusCode()+"\"}";
			    return null;
		    }
		    HttpEntity entity = response.getEntity();
		    if (entity != null) {
			    lines = EntityUtils.toString(entity, "utf-8");
		        return lines;
		    }
	    } catch (IOException e) {
	    	logger.error("request url=" + url + ", exception, msg=" + e.getMessage());
		    e.printStackTrace();
		    lines="{\"code\":500}";
		} finally {
		    if (response != null) try {
		        response.close();
		    } catch (IOException e) {
		    	lines="{\"code\":500}";
		        e.printStackTrace();
		    }
		}
		return lines;
	}
}
